<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager_modules extends Admin_Controller {

	protected $table = ADMIN_MODULE_TABLE; // Also configed in model
	
	// Config List view
	protected $list_view_order_name = 'name';
	protected $list_view_order_direction = 'ASC';
	protected $list_view_searched_fields = array(
			'name'			=>	'name',
		);
		
	protected $fields =  array(
			'parent_id'	=>	array(
								'text'	=>	'Nhóm',
								'type'	=>	'select',
								'select_config' => array(
									'referred_table_name' 	=> ADMIN_MODULE_GROUP_TABLE,
									'referred_field_name' 	=> 'name',
									'select_default_text' 	=> 'Chọn nhóm',
								),
								'list_view'=>array(
									'is_show_link'	=>	true
								),
								'form_view' => array(
								)
							),	
			'name'		=>	array(
								'text'		=>	'Tên module',
								'type'		=>	'text',
								'list_view' => array(
									'is_show_link'	=>	true
								),
								'form_view' => array(
									'type'	=>	'text',
									'validate_type'	=>	'required : true',
									'check_duplicate'	=>	true
								)
							),
			);
	/** End: Config **/

	/*------------------------------------ Admin Control Panel ------------------------------------*/
	
	public function admincp_ajaxUpdateStatus(){
		admin_check_permission($this->module_id,'w',true);
		if($this->input->post('status')==0){
			$status = 1;
		}else{
			$status = 0;
		}
		$data = array(
			$this->status_name=>$status
		);
		$this->db->where('id', $this->input->post('id'));
		if($this->db->update($this->table, $data)){
			//Xét quyền cho Group
			$this->load->model('account_groups/account_groups_model');
			$list_group = $this->account_groups_model->list_groups();
			foreach($list_group as $v){
				if($status==0){
					$pos = strpos($v->permission,$this->input->post('id'));
					if($pos !== false){
						$sub_str = substr($v->permission,$pos-1,6);
						$new_perm = str_replace($sub_str,'',$v->permission);
						$this->db->where('id',$v->id);
						$this->db->update(ADMIN_GROUP_TABLE,array('permission'=>$new_perm));
					}
				}else{
					$pos = strpos($v->permission,'0');
					if($pos !== false){
						$new_perm = substr($v->permission,$pos+2,3);
						$this->db->where('id',$v->id);
						$this->db->update(ADMIN_GROUP_TABLE,array('permission'=>$v->permission.','.$this->input->post('id').'|'.$new_perm));
					}
				}
			}
			
			//Xét quyền lại cho từng Users
			$this->load->model('accounts/accounts_model');
			$list_account = $this->accounts_model->list_accounts();
			foreach($list_account as $v){
				if($status==0){
					$pos = strpos($v->permission,$this->input->post('id'));
					if($pos !== false){
						$sub_str = substr($v->permission,$pos-1,6);
						$new_perm = str_replace($sub_str,'',$v->permission);
						$this->db->where('id',$v->id);
						$this->db->update(ADMIN_USER_TABLE,array('permission'=>$new_perm));
					}
				}else{
					$group_account = $this->account_groups_model->getDetailManagement($v->group_id);
					$pos = strpos($group_account->permission,'0');
					if($pos !== false){
						$new_perm = substr($v->permission,$pos+2,3);
						$this->db->where('id',$v->id);
						$this->db->update(ADMIN_USER_TABLE,array('permission'=>$v->permission.','.$this->input->post('id').'|'.$new_perm));
					}
				}
			}
		}
		
		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('ajax_updateStatus',$update);
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	/*------------------------------------ End FRONTEND --------------------------------*/
}